package com.smag.web.controller.system;

import com.smag.common.annotation.Log;
import com.smag.common.config.RuoYiConfig;
import com.smag.common.core.controller.BaseController;
import com.smag.common.core.domain.AjaxResult;
import com.smag.common.core.domain.entity.FarmProduct;
import com.smag.common.core.domain.entity.SysUser;
import com.smag.common.core.page.TableDataInfo;
import com.smag.common.enums.BusinessType;
import com.smag.common.utils.SecurityUtils;
import com.smag.common.utils.file.FileUploadUtils;
import com.smag.common.utils.file.MimeTypeUtils;
import com.smag.common.utils.poi.ExcelUtil;
import com.smag.system.domain.dto.ProductQueryDto;
import com.smag.system.service.FarmProductService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/system/product")
public class SysProductController extends BaseController {
    @Resource
    private FarmProductService farmProductService;

    @GetMapping
    public AjaxResult listProduct() {
        return success(farmProductService.selectAllProduct());
    }

    /**
     * 查询农产品管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductQueryDto productQueryDto) {
        startPage();
        if (SysUser.isAdmin(SecurityUtils.getUserId()))
            return getDataTable(farmProductService.selectFarmProductList(productQueryDto, null));
        return getDataTable(farmProductService.selectFarmProductList(productQueryDto, SecurityUtils.getUserId()));
    }

    @PreAuthorize("@ss.hasPermi('system:product:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long productId) {
        return success(farmProductService.selectFarmProductById(productId));

    }

    /**
     * 导出农产品管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:export')")
    @Log(title = "农产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductQueryDto productQueryDto) {
        List<FarmProduct> list = null;
        if (SysUser.isAdmin(SecurityUtils.getUserId())) {
            list = farmProductService.selectFarmProductList(productQueryDto, null);
        } else {
            list = farmProductService.selectFarmProductList(productQueryDto, SecurityUtils.getUserId());
        }
        ExcelUtil<FarmProduct> util = new ExcelUtil<FarmProduct>(FarmProduct.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    @PostMapping("/avatar")
    @Log(title = "农产品管理", businessType = BusinessType.EXPORT)
    public AjaxResult avatar(@RequestParam("productId") Long productId, @RequestParam("avatarfile") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (farmProductService.updateTechAvatar(productId, avatar)) {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                return ajax;
            } else {
                return error("上传图片失败，请联系管理员");
            }
        }
        return error("上传图片异常，请联系管理员");
    }


    /**
     * 新增农产品管理
     */
    @PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "农产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FarmProduct farmProduct) {
        return toAjax(farmProductService.insertFarmProduct(farmProduct));
    }

    /**
     * 修改农产品管理
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "农产品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FarmProduct farmProduct) {
        return toAjax(farmProductService.updateFarmProduct(farmProduct));
    }

    /**
     * 删除农产品管理
     */
    @PreAuthorize("@ss.hasPermi('system:product:remove')")
    @Log(title = "农产品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(farmProductService.deleteFarmProductByIds(ids));
    }
//    @PreAuthorize("@ss.hasPermi('system:product:remove')")
//    @Log(title = "农产品管理", businessType = BusinessType.DELETE)
    @PutMapping("/{ids}")
    public AjaxResult shelvesProduct(@PathVariable Long[] ids) {
        return toAjax(farmProductService.updateFarmProductStatus(ids));
    }
}
